package com.xb.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.xb.common.helper.JwtHelper;
import com.xb.common.helper.MD5;
import com.xb.common.result.Result;
import com.xb.common.result.ResultCodeEnum;
import com.xb.model.system.SysUser;

import com.xb.model.vo.LoginVo;
import com.xb.model.vo.RouterVo;
import com.xb.model.vo.SysUserQueryVo;

import com.xb.system.exception.XBException;
import com.xb.system.mapper.SysUserMapper;

import com.xb.system.service.SysMenuService;
import com.xb.system.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author xb
 * @since 2023-08-23
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {


    @Resource
    private SysMenuService sysMenuService;

    //分页查询
    @Override
    public IPage<SysUser> selectPage(Page<SysUser> pageParam, SysUserQueryVo userQueryVo) {
        IPage<SysUser> pageModel = baseMapper.selectPage(pageParam, userQueryVo);
        return pageModel;
    }

    //用户状态修改
    @Override
    public void updateStatus(Long id, Integer status) {
        //获取修改的用户
        SysUser sysUser = baseMapper.selectById(id);
        //更改状态
        sysUser.setStatus(status);
        baseMapper.updateById(sysUser);
    }

    //登录核实
    @Override
    public Result<Map<String, Object>> loginCheck(LoginVo loginVo) {

        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",loginVo.getUsername());
        SysUser sysUser = this.getOne(queryWrapper);

        //用户名不存在
        if(null == sysUser) {
            throw new XBException(ResultCodeEnum.ACCOUNT_ERROR);
        }
        //密码错误
        if(!MD5.encrypt(loginVo.getPassword()).equals(sysUser.getPassword())) {
            throw new XBException(ResultCodeEnum.PASSWORD_ERROR);
        }
        //用户禁用
        if(sysUser.getStatus() == 0) {
            throw new XBException(ResultCodeEnum.ACCOUNT_STOP);
        }

        //生成token
        Map<String, Object> map = new HashMap<>();
        map.put("token", JwtHelper.createToken(sysUser.getId(), sysUser.getUsername()));
        return Result.ok(map);
    }

    //获取用户信息
    @Override
    public Map<String, Object> getUserInfo(String username) {
        Map<String, Object> result = new HashMap<>();
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        SysUser sysUser = this.getOne(queryWrapper);
        //根据用户id获取菜单权限值
        List<RouterVo> routerVoList = sysMenuService.findUserMenuList(sysUser.getId());
        //根据用户id获取用户按钮权限
        List<String> permsList = sysMenuService.findUserBtnPermsList(sysUser.getId());

        result.put("name", sysUser.getName());
        result.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");

        result.put("roles", new HashSet<>());
        result.put("buttons", permsList);
        result.put("routers", routerVoList);
        return result;
    }

    //根据用户名获取用户
    @Override
    public SysUser getByUsername(String username) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        SysUser sysUser = this.getOne(queryWrapper);
        return sysUser;
    }


}
